package com.taikang.hmp.hbusiness.diabetes.measure.warn.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.hbusiness.diabetes.measure.warn.bean.Warning;
import com.taikang.hmp.hbusiness.diabetes.measure.warn.service.IWarnService;

/** 
 * warn service implement
 * author:qrp
 * time:2015年7月24日 上午11:25:09
 */
public class WarnServiceImpl extends BaseServiceImpl implements IWarnService{

	public List<Dto> getWarnList(Dto dto) {
		Warning warning = new Warning();
		warning.setCustomer_id(dto.getAsString("customerid"));
		warning.setDates(dto.getAsString("date"));
		List<Warning> info = super.getNewDao().queryForList("warning_space.getWarnList", warning);
		List<Dto> resultList = new ArrayList<Dto>();
		if(info.size() > 0){
			for (int i = 0; i < info.size(); i++) {
				Dto result = new BaseDto();
				result.put("date", info.get(i).getDates());
				result.put("warnType", info.get(i).getWarntype_cd());
				result.put("warnMsg", info.get(i).getWarnmsg());
				result.put("warnId", info.get(i).getWarn_id());
				resultList.add(result);
			}
		}
		return resultList;
	}

	public boolean setWarnState(Dto dto) {
		Warning warning = new Warning();
		warning.setCustomer_id(dto.getAsString("customerid"));
		warning.setWarn_id(dto.getAsString("warnid"));
		warning.setIs_read(dto.getAsString("isread"));
		try {
			super.getNewDao().update("warning_space.setWarnState", warning);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public int getWarnCount(Dto dto) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("customerid", dto.getAsString("customerid"));
		map.put("date", dto.getAsString("date"));
		return (Integer)super.getNewDao().queryForObject("warning_space.getWarnCount", map);
	}

	public void saveWarn(JSONObject jsonObj) {
		Warning warning = new Warning();
		warning.setCustomer_id(jsonObj.getString("customer_id"));
		warning.setIs_read(jsonObj.getString("isread"));
		warning.setWarnmsg(jsonObj.getString("warnmsg"));
		warning.setDates(jsonObj.getString("date"));
		warning.setWarn_id(java.util.UUID.randomUUID().toString());
		warning.setWarntype_cd(jsonObj.getString("warntypecd"));//提醒类型
		warning.setOperate_date(new Date());
		try {
			super.getNewDao().insert("warning_space.insert_warn", warning);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
